package com.topboya.linker.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.topboya.linker.R;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.ArrayList;

public class FeekbackAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "SelectFileAdapter:";
    private ArrayList<String> feeknamelsit;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private boolean mIsSelectable = false;
    private Context mContext;

    public FeekbackAdapter(Context mContext, ArrayList<String> feeknamelsit) {

        if (feeknamelsit == null) {
            throw new IllegalArgumentException("model Data must not be null");
        }
        this.feeknamelsit = feeknamelsit;
        this.mContext = mContext;
    }

    //更新adpter的数据和选择状态
    public void updateDataSet(ArrayList<String> feeknamelsit) {
        
        this.feeknamelsit = feeknamelsit;
        mSelectedPositions = new SparseBooleanArray();

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.feek_back_item, viewGroup, false);
        return new FeekbackAdapter.ListItemViewHolder(itemView);
    }

    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {

        mSelectedPositions.put(position, isChecked);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {

        return mSelectedPositions.get(position);
    }

    //根据位置判断条目是否可选
    private boolean isSelectable() {

        return mIsSelectable;
    }
    //设置给定位置条目的可选与否的状态
    private void setSelectable(boolean selectable) {

        mIsSelectable = selectable;
    }


    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {

        //设置条目状态

        if (isItemChecked(i)){

            ((ListItemViewHolder) holder).tv_feekback_label.setBackgroundResource(R.drawable.selected);

        }else {

            ((ListItemViewHolder) holder).tv_feekback_label.setBackgroundResource(R.drawable.noselected);
        }

//        if (null !=itemClickListener){
//
//             itemClickListener.onClick(holder.itemView ,i,count);
//
//        }
    }

    @Override
    public int getItemCount() {

        return feeknamelsit == null ? 0 : feeknamelsit.size();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        TextView tv_feekback_label;

        ListItemViewHolder(View view) {

            super(view);
            this.tv_feekback_label = view.findViewById(R.id.tv_feekback_label);

        }

    }

    private FeekbackAdapter.OnItemClickListener itemClickListener;

    public void setOnItemClickListener(FeekbackAdapter.OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }

    public interface  OnItemClickListener{

        void onClick(View v, int position, int count);
    }

}
